package com.xiaolong.websocket;

import java.security.Principal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;

@Controller
public class WebScocketController {
	
	@Autowired
	private SimpMessagingTemplate messagingTemplate;
	
	@MessageMapping("/welcome")
	@SendTo("/topic/getResponse")
	public ResultData say(MessageData message){
		return new ResultData("hi!"+message.getMessage());
	}
	
	@MessageMapping("/chat")
	public void handleChat(Principal principal,String message){
		if(principal.getName().equals("user")){
			messagingTemplate.convertAndSendToUser("admin", "/queue/notifications", principal.getName()+"-send:"+message);
		}else{
			messagingTemplate.convertAndSendToUser("user", "/queue/notifications", principal.getName()+"-send:"+message);
		}
	}
}
